package com.example.sjjg.animator

import android.animation.*
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.AnimationDrawable
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.animation.*
import com.example.sjjg.animator.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    lateinit var binding : ActivityMainBinding

    override fun onResume() {
        super.onResume()
        Log.e("MainActivity","onResume")
    }

    override fun onStart() {
        super.onStart()
        Log.e("MainActivity","onStart")
    }

    override fun onRestart() {
        super.onRestart()
        Log.e("MainActivity","onRestart")
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e("MainActivity","onCreate")
        binding = ActivityMainBinding.inflate(LayoutInflater.from(applicationContext))
        setContentView(binding.root)
        binding.valueAnimator.setOnClickListener { onValueAnimatorClicked(it) }
        binding.keyFrame.setOnClickListener { onKeyFrameClicked(it) }
        binding.viewAnimate.setOnClickListener { onViewAnimate(it) }
        binding.frameBtn.setOnClickListener { onFrameAnimationClicked(it) }
        binding.xmlTranslation1Animator.setOnClickListener { onXmlTranslation1AnimatorClicked(it) }
        binding.xmlTranslation2Animator.setOnClickListener { onXmlTranslation2AnimatorClicked(it) }
        binding.xmlRotationAnimator.setOnClickListener { onXmlRotationAnimatorClicked(it) }
        binding.xmlScaleAnimator.setOnClickListener { onXmlScaleAnimatorClicked(it) }
        binding.xmlAlphaAnimator.setOnClickListener { onXmlAlphaAnimatorClicked(it) }
        binding.xmlSetAnimator.setOnClickListener { onXmlSetAnimatorClicked(it) }
        binding.constraintLayout2.setOnClickListener { onConstraintLayout2Clicked(it) }

        /*if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            val am = AnimatorInflater.loadStateListAnimator(baseContext,R.xml.animate_scale)
            binding.valueAnimator.stateListAnimator = am
        }*/
    }
    private fun onValueAnimatorClicked(v : View){
        val animatorUpdate = object : ValueAnimator.AnimatorUpdateListener {
            override fun onAnimationUpdate(animation: ValueAnimator?) {
                val value = animation?.animatedValue
                Log.e("MainActivity","AnimatorUpdateListener value = $value")
            }
        }
        val animatorListener = object : Animator.AnimatorListener{
            override fun onAnimationStart(animation: Animator?) {
                Log.e("MainActivity","AnimatorListener onAnimationStart")
            }

            override fun onAnimationEnd(animation: Animator?) {
                Log.e("MainActivity","AnimatorListener onAnimationEnd")
            }

            override fun onAnimationCancel(animation: Animator?) {
                Log.e("MainActivity","AnimatorListener onAnimationCancel")
            }

            override fun onAnimationRepeat(animation: Animator?) {
                Log.e("MainActivity","AnimatorListener onAnimationRepeat")
            }
        }
        val animatorAdapter = object : AnimatorListenerAdapter(){
            override fun onAnimationStart(animation: Animator?, isReverse: Boolean) {
                super.onAnimationStart(animation, isReverse)
                Log.e("MainActivity","AnimatorListenerAdapter onAnimationStart")
            }

            override fun onAnimationEnd(animation: Animator?, isReverse: Boolean) {
                super.onAnimationEnd(animation, isReverse)
                Log.e("MainActivity","AnimatorListenerAdapter onAnimationEnd")
            }
        }
        ValueAnimator.ofFloat(0f, 100f).apply {
            addUpdateListener(animatorUpdate)
            addListener(animatorAdapter)
            interpolator = DecelerateInterpolator()
            duration = 1000
            start()
        }
        //动画1
        val end = if(binding.animatorTitle.translationX < binding.root.measuredWidth / 2) binding.root.measuredWidth / 2f else 0f
        val translation = ObjectAnimator.ofFloat(binding.animatorTitle, "translationX", end).apply {
            duration = 1000
            interpolator = AccelerateInterpolator()
        }
        //动画2
        val fade1 = ObjectAnimator.ofFloat(binding.animatorTitle, "alpha", 1f, 0.5f).apply {
            duration = 1000
        }
        //动画3
        val fade2 = ObjectAnimator.ofFloat(binding.animatorTitle, "alpha", 0.5f, 1f).apply {
            duration = 1000
        }

        AnimatorSet().apply {
            /*
            play(fade1)                       //顺序播放
            play(fade2)
            play(translation)
            */
            play(fade1).before(fade2)           //之前播放
            play(translation).after(fade2)      //在之后播放
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                val startColor = Color.parseColor("#000000")
                val endColor = Color.parseColor("#ff0000")
                val color = ObjectAnimator.ofArgb(binding.animatorTitle, "textColor", startColor,endColor).apply {
                    duration = 1000
                    interpolator = AccelerateDecelerateInterpolator()
                }
                play(translation).with(color)         //同时播放
            }
            start()
        }
    }
    private fun onViewAnimate(v : View){
        val x = binding.animatorTitle.translationX
        binding.animatorTitle.animate().translationX(x + 20f)
    }
    private fun onKeyFrameClicked(v : View){
        val kf0 = Keyframe.ofFloat(0f, 0f)      //第1帧
        val kf1 = Keyframe.ofFloat(0.5f, 360f)  //中间帧
        val kf2 = Keyframe.ofFloat(1f, 0f)      //最后1帧
        val pvhRotation = PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2)
        ObjectAnimator.ofPropertyValuesHolder(binding.animatorTitle, pvhRotation).apply {
            duration = 5000
//            interpolator = OvershootInterpolator()
//            interpolator = CycleInterpolator(0.5f)
            interpolator = AccelerateDecelerateInterpolator()
            start()
        }
    }
    private fun onXmlTranslation1AnimatorClicked(v : View){
        val update = object : ValueAnimator.AnimatorUpdateListener {
            override fun onAnimationUpdate(animation: ValueAnimator?) {
                val value = animation?.animatedValue as Float
                Log.e("MainActivity","AnimatorUpdateListener value = $value")
                binding.animatorTitle.translationX = value
            }
        }
        (AnimatorInflater.loadAnimator(baseContext, R.animator.animator_translation1) as ValueAnimator).apply {
            setTarget(binding.animatorTitle)
            addUpdateListener(update)
            start()
        }
    }
    private fun onXmlTranslation2AnimatorClicked(v : View){
        (AnimatorInflater.loadAnimator(baseContext, R.animator.animator_translation2) as ObjectAnimator).apply {
            setTarget(binding.animatorTitle)
            start()
        }
    }
    private fun onXmlRotationAnimatorClicked(v : View){
        (AnimatorInflater.loadAnimator(baseContext, R.animator.animator_rotation) as ObjectAnimator).apply {
            setTarget(binding.animatorTitle)
            start()
        }
    }
    private fun onXmlScaleAnimatorClicked(v : View){
        (AnimatorInflater.loadAnimator(baseContext, R.animator.animator_scale) as ObjectAnimator).apply {
            setTarget(binding.animatorTitle)
            start()
        }
    }
    private fun onXmlAlphaAnimatorClicked(v : View){
        (AnimatorInflater.loadAnimator(baseContext, R.animator.animator_alpha) as ObjectAnimator).apply {
            setTarget(binding.animatorTitle)
            start()
        }
    }
    private fun onXmlSetAnimatorClicked(v : View){
        (AnimatorInflater.loadAnimator(baseContext, R.animator.animator_set) as AnimatorSet).apply {
            setTarget(binding.animatorTitle)
            start()
        }

    }
    fun onFrameAnimationClicked(v : View){
        val frameAnimation = binding.frameImage.background as AnimationDrawable
        frameAnimation.start()
    }
    fun onConstraintLayout2Clicked(v : View){
        val intent = Intent(baseContext,Constraint2Activity::class.java)
        startActivity(intent)
    }
}

